import {IMock} from './i-mock';
import {MockConfigItem} from '../../entity/mock-config-item';
import {MockUtils} from '../mock-utils';
import {AlertMessage} from '../../entity/alert-message';

export class StringMock implements IMock {
    getBaseTypeMockConfigDisplay(mockConfigItem: MockConfigItem): string {
        switch (mockConfigItem.mockType) {
            case 0:
            case 1:
                if (!mockConfigItem.value) {
                    return 'null';
                }
                const str = '\"' + mockConfigItem.value + '\"';
                if (str.length > 28) {
                    mockConfigItem.mockType = 1;
                }
                return str;
            case 2:
                return '@random(singleRowList)';
            case 3:
                return '@random(multiRowList)';
            case 4:
                return '@randomStr(' + mockConfigItem.mockValueStart + ', ' + mockConfigItem.mockValueEnd + ')';
            default:
                return mockConfigItem.value;
        }
    }

    onSave(mockConfigItem: MockConfigItem): boolean {
        if (mockConfigItem.mockType === 4) {
            // 需要检查开始、结束是否为空
            if (typeof (mockConfigItem.mockValueStart) === 'undefined') {
                MockUtils.setMockInfo(AlertMessage.error('随机N个字符串-开始值，不能为空！'));
                return false;
            }
            if (typeof (mockConfigItem.mockValueEnd) === 'undefined') {
                MockUtils.setMockInfo(AlertMessage.error('随机N个字符串-结束值，不能为空！'));
                return false;
            }
        }
        return true;
    }
}
